package gl.uitls;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.telephony.SmsManager;
import android.util.Log;

public class AndroidSystemUtils {
	public static void Call(Context context, String phonenumber) {
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
				+ phonenumber));
		context.startActivity(intent);
	}

	public static void sendSMS(Context context, String number, String msg) {
		String SENT = "sms_sent";
		String DELIVERED = "sms_delivered";

		PendingIntent sentPI = PendingIntent.getActivity(context, 0, new Intent(
				SENT), 0);
		PendingIntent deliveredPI = PendingIntent.getActivity(context, 0,
				new Intent(DELIVERED), 0);

		context.registerReceiver(new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Log.i("====>", "Activity.RESULT_OK");
					Tip.show("发送成功");
					return;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Log.i("====>", "RESULT_ERROR_GENERIC_FAILURE");
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Log.i("====>", "RESULT_ERROR_NO_SERVICE");
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Log.i("====>", "RESULT_ERROR_NULL_PDU");
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Log.i("====>", "RESULT_ERROR_RADIO_OFF");
					break;
				}
				Tip.show("发送短信失败");
			}
		}, new IntentFilter(SENT));

		context.registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Log.i("====>", "RESULT_OK");
					Tip.show("发送成功");
					break;
				case Activity.RESULT_CANCELED:
					Log.i("=====>", "RESULT_CANCELED");
					Tip.show("已经取消发送短信");
					break;
				}
			}
		}, new IntentFilter(DELIVERED));

		SmsManager smsm = SmsManager.getDefault();
		smsm.sendTextMessage(number, null, msg, sentPI, deliveredPI);
	}
}
